from .x23uscom import *
from .spider import *
import os
# Create your views here.

class DownLoad:
    def __init__(self,url):
        self.url = url
        self.info_list = self.book()
        #print(self.chapter_id)
        
    def book(self):
        
        book_url = self.url
        book_name = '<meta name="og:novel:book_name" content="(.*?)"/>'
        book_author = '<meta name="og:novel:author" content="(.*?)"/>'
        book_info = '<td class="L"><a href="(.*?)">(.*?)</a></td>'

        x = Spider(url = book_url)
        info_list = x.info(book_name = book_name,book_author = book_author,book_info = book_info)
        #print(info_list)
        return info_list

        
    def chapter(self):
        book_name = self.info_list['book_name'][0]
        book_author = self.info_list['book_author'][0]
        book_info = self.info_list['book_info'][:5]
        for i in range(len(book_info)):
            content_url = '{}{}'.format(self.url,book_info[i][0])
            chapter_title = '<title>(.*?)-(.*?)-(.*?)</title>'
            chapter_content = '<dd id="contents">(.*)</dd>'
            x = Spider(url = content_url)
            info_dict = x.info(chapter_title = chapter_title,chapter_content = chapter_content)
            info_dict['chapter_content'][0] = info_dict['chapter_content'][0].replace('&nbsp;','').replace('<br />','').replace('顶点小说 Ｘ２３ＵＳ．ＣＯＭ更新最快','')
            chapter = info_dict['chapter_title'][0][1]
            print(chapter)
            content = info_dict['chapter_content'][0]
            
            with open('{}.txt'.format(book_name),'a') as f:
                f.write(chapter)
                f.write('\n')
                f.write(content)
                f.write('\n')
        path = os.getcwd()+'\\{}.txt'.format(book_name)
        with open('{}'.format(path),'r') as r:
            content_r = r.read()
        return content_r
    

if __name__ =='__main__':

    x = chapter()










    
